-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keras Interface #54
base: main
Are you sure you want to change the base?
Keras Interface #54
Conversation
@@ -127,6 +127,7 @@ venv/ | |||
ENV/ | |||
env.bak/ | |||
venv.bak/ | |||
qiboml/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
qiboml/ |
better not to ignore this
{ | ||
// Usare IntelliSense per informazioni sui possibili attributi. | ||
// Al passaggio del mouse vengono visualizzate le descrizioni degli attributi esistenti. | ||
// Per altre informazioni, visitare: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Debugger Python: File corrente", | ||
"type": "debugpy", | ||
"request": "launch", | ||
"program": "${file}", | ||
"justMyCode": false, | ||
"console": "integratedTerminal", | ||
"env": { | ||
"ON_HEROKU": "0", | ||
"PYTEST_ADDOPTS": "-c pytest.ini", | ||
"ECHO_SQL_QUERIES": "1" | ||
}, | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is probably some vscode leftover, better to remove it
|
||
import tensorflow as tf | ||
import numpy as np | ||
from qibo import Circuit, gates | ||
|
||
|
||
@tf.custom_gradient | ||
def custom_operation(): | ||
output = | ||
|
||
def grad_fn() | ||
|
||
|
||
return output, grad_fn | ||
|
||
|
||
|
||
class MyLayer(tf.keras.layers.Layer): | ||
|
||
def __init__(self): | ||
super(MyLayer, self).__init__(): | ||
self.circuit = self.circuit() | ||
|
||
self.weights = self.add_weights(name='w', shape=(4,), initializer="random_normal") | ||
|
||
|
||
def circuit(self): | ||
c = Circuit(2) | ||
c.add(gates.X(0)) | ||
c.add(gates.RX(1, theta=0.5)) | ||
|
||
def call(self, x): | ||
self.circuit() | ||
|
||
def |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this has to be removed as well
[pytest] | ||
env = | ||
TESTING=true | ||
ENV=local |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
even this has to go I would say
@@ -44,8 +46,9 @@ def __post_init__( | |||
self._circuit.add(gates.RY(q, theta=0.0, trainable=False)) | |||
|
|||
def _set_phases(self, x: ndarray): | |||
for gate, phase in zip(self._circuit.parametrized_gates, x.ravel()): | |||
gate.parameters = phase | |||
phase = tf.reshape(x, [-1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we cannot have tf
functions here, this has to work for pytorch
as well and eventually jax
when we introduce the Flax
interface
def draw( | ||
self, | ||
): | ||
breakpoint() | ||
print("ciao") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def draw( | |
self, | |
): | |
breakpoint() | |
print("ciao") |
import qibo | ||
from qibo import gates, Circuit | ||
import numpy as np | ||
import tensorflow as tf | ||
|
||
|
||
def random_subset(nqubits, k): | ||
return np.random.choice(range(nqubits), size=(k,), replace=False).tolist() | ||
|
||
|
||
nqubits = 3 | ||
dim = 2 | ||
backend = "tensorflow" | ||
|
||
c = Circuit(nqubits) | ||
c.add(gates.X(0)) | ||
c.add(gates.X(1)) | ||
c.add(gates.Z(1)) | ||
c.add(gates.CNOT(0, 1)) | ||
c.add(gates.RX(0, theta=0.4)) | ||
|
||
random_choice = random_subset(nqubits, dim) | ||
print(f"Scelta random {random_choice}") | ||
result = c().probabilities() | ||
print(result) | ||
|
||
|
||
tensor = tf.random.uniform((2, nqubits), minval=0, maxval=2, dtype=tf.int32) | ||
print(f"Tensore: {tensor}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this test covers some case that is not tested in tests/
then better to add it there, otherwise we should remove this
# "pytorch", | ||
# "jax", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# "pytorch", | |
# "jax", | |
"pytorch", | |
"jax", |
reactivate these to check that pytorch
and jax
are still working
] | ||
|
||
FRONTENDS = [ | ||
"pytorch", | ||
# "pytorch", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# "pytorch", | |
"pytorch", |
same here
def build_linear_layer_adding(frontend, q_model): | ||
model = frontend.keras.Sequential() | ||
model.add( | ||
frontend.keras.layers.Dense( | ||
5, | ||
name="Dense0", | ||
activation="relu", | ||
input_shape=(input_size,), | ||
) | ||
) | ||
model.add(q_model) | ||
model.add( | ||
frontend.keras.layers.Dense( | ||
12, | ||
name="Dense1", | ||
activation="relu", | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's this for? can't you use the build_sequential
below?
This is the implementation of the Keras Interface. This PR supports only the differentiation with TensorFlow as backend.
In a future PR, I plan to enhance this Keras Interface by adding support for other types of differentiation, such as JAX.
As stated in issue #47, I had to change the Binary encoding implementation to use Symbolic Tensors.